import { getInfo as memberInfo } from '../../../api/shareCenter'
import { recharge } from '../../../api/deposit';
import { getBankInfo } from '../../../api/enterprise';
const util = require('../../../utils/util.js');
import {
	submit,
	query
} from '../../../api/payment';
let app = getApp()

Page({
	data: {
		amount1:'',
		paymentVo:[1,2,3],
		balance: 0,
		point: 0,
		amount: "",
		maxlength: 10,
		disabled: true,
		check:'',
		paymentType:'weixinMiniPayPlugin',
		bank:'',
		bankNum:'',
		bankName:'',
		bank:'',
	},
	onLoad: function (options) {
		this.getBalance();
		this.getBankInfo();
		getBankInfo({mchId:app.globalData.mchId}).then(res=>{
			console.log(res.data.bankNum,"321321")
			this.data.bankNum = res.data.bankNum;
			this.data.bankName = res.data.bankName;
			this.data.bank = res.data.bank;
			this.setData({
				bankNum  : this.data.bankNum,
				bankName :this.data.bankName,
				bank:this.data.bank
			})
		})

	},
	getBankInfo(){
		console.log("321321");
	},
	getBalance() {
	
			memberInfo().then(res => {
				this.data.balance = res.data.balance
				this.data.point = ( res.data.point/100).toFixed(2)
				this.setData({
					balance: this.data.balance,
					point: this.data.point
				})
			})
		
	},
	getInput(e) {
		let amount = e.detail.value
		let maxlength = amount.indexOf('.') + 3 // 限制小数点后只能输入两位
		if (maxlength == 2) {
			maxlength = 10
		}
		let disabled = util.isNull(amount)
		this.setData({amount, maxlength, disabled})
	},
	wholeCashOut() {
		let amount = this.data.balance / 100
		this.setData({amount, disabled: false})
	},

	changeClick(data){
		console.log(data)
		this.data.paymentType = data.currentTarget.dataset.type
		this.setData({
			paymentType:this.data.paymentType
		})
	},
	paymentClick(){

		var _this = this
		console.log(_this.data.amount)
		if(_this.data.amount<=0||util.isNull(_this.data.amount)){
					wx.showToast({
					    icon: "none",
				        title: '请输入充值金额',
						})
				return;
		}
		recharge({
			amount:_this.data.amount,
			paymentPluginId:_this.data.paymentType
		}).then(res=>{
			console.log(res.data.sn)
			if(this.data.paymentType=='bankPayPlugin'){
				wx.showToast({
					    icon: "none",
				        title: '充值成功',
					  })
			setTimeout(()=>{
				// wx.redirectTo({
                //     // url: '/pages/tabBar/mine/mine',
                //     url: '/pages/member/warehouse/warehouse',
                //   })
                wx.navigateBack({
                    delta: 1
                  })
			},1000)
			}else if(this.data.paymentType=='pointPayPlugin')
			{
				wx.redirectTo({
					icon: "none",
					title: '充值成功',
				  })
				  setTimeout(()=>{
					// wx.redirectTo({
                    //     // url: '/pages/tabBar/mine/mine',
                    //     url: '/pages/member/warehouse/warehouse',
                    //   })
                    wx.navigateBack({
                        delta: 1
                      })
				},1000)
		}
			else if(this.data.paymentType=='weixinMiniPayPlugin'){
				_this.submitClick(res.data.sn)
			}
		});
	},
	queryClick() {
		var _this = this
		query({
		  sn: data
		}).then(res => {
		// wx.navigateTo({
		// 	url: '/pages/tabBar/mine/mine',
		// })
		wx.navigateBack({
			delta: 1
		  })
		}).catch(error => {
		  wx.showToast({
			icon: "none",
			title: error.content,
		  })
		})
	  },

	submitClick(data){
		submit({
			paymentPluginId:'weixinMiniPayPlugin',
			sn:data
		}).then(res=>{
			wx.hideLoading()
			if (res.type == 'success') {
			  wx.requestPayment({
				'timeStamp': res.data.timeStamp,
				'nonceStr': res.data.nonceStr,
				'package': res.data.package,
				'signType': res.data.signType,
				'paySign': res.data.paySign,
				'success': function (red) {
				  _this.queryClick(data)
				},
				'fail': function (red) {
				  wx.showToast({
					icon: "none",
					title: '支付失败',
				  })
				//   wx.redirectTo({
				// 	url: '/pages/member/order/details?id=' + _this.data.orderId,
				//   })
				wx.navigateBack({
					delta: 1
				  })
				  	
				}
			  })
			} else {
			  wx.showToast({
				icon: "none",
				title: '支付失败',
			  })
			//   wx.redirectTo({
			// 	url: '/pages/member/order/details?id=' + _this.data.orderId,
			//   })
			wx.navigateBack({
				delta: 1
			  })
			
		}
	})},




})


